﻿using System;
using StockholmsStad.Runtime.Serialization.DataContract;
using StockholmsStad.Windows.UI.Facade.ParvisService;
using StockholmsStad.Runtime.Serialization.DataContract.Enums;
using System.Collections.Generic;
using StockholmsStad.Windows.UI.Facade.TicketService;
using StockholmsStad.Windows.UI.Facade.UserService;

namespace StockholmsStad.Windows.UI.Facade
{
    public class UserFacade : FacadeBase
    {
        public UserFacade(User user)
            : base(user)
        {
        }

        public User Logon()
        {
            var client = new WinServiceClient();
            try
            {
                var user = client.Login(CurrentUser);
                client.Close();
                return user;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        public List<UserRole> GetRoles()
        {
            var client = new WinServiceClient();

            try
            {
                var list = new List<UserRole>();
                var roles = client.GetRoles(CurrentUser);
                client.Close();
                list.AddRange(roles);
                return list;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        public void SetUserInactive(string username)
        {
            var client = new WinServiceClient();

            try
            {
                client.SetUserInactive(CurrentUser, username);
                client.Close();
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        public void DeleteUser(int userId)
        {
            var client = new UserServiceClient();

            try
            {
                client.RemoveParvisUser(CurrentUser, userId);
                client.Close();
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        public List<ParvisUser> GetUsers(bool includeInactiveUsers)
        {
            var client = new WinServiceClient();

            try
            {
                var list = new List<ParvisUser>();
                var users = client.GetUsers(CurrentUser, includeInactiveUsers);
                client.Close();
                list.AddRange(users);
                return list;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        public int AddUser(ParvisUser user)
        {
            var client = new WinServiceClient();

            try
            {
                var id = client.AddUser(CurrentUser, user);
                client.Close();
                return id;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        public void UpdateUser(ParvisUser user)
        {
            var client = new WinServiceClient();

            try
            {
                client.UpdateUser(CurrentUser, user);
                client.Close();
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        public ParvisUser GetUser(int parvisUserId)
        {
            var client = new WinServiceClient();

            try
            {
                var user = client.GetParvisUser(CurrentUser, parvisUserId);
                client.Close();
                return user;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        public void SendForgottenPassword(ParvisUser user)
        {
            var client = new TicketServiceClient();

            try
            {
                client.SendForgottenPassword(CurrentUser, user);
                client.Close();
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }
    }
}
